package com.quickcancellation.crm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.quickcancellation.crm.config.LogAspect;
import com.quickcancellation.crm.pojo.dto.PageDto;
import com.quickcancellation.crm.pojo.dto.PageParamsDto;
import com.quickcancellation.crm.pojo.dto.ResponseDto;
import com.quickcancellation.crm.pojo.entity.Salearea;
import com.quickcancellation.crm.pojo.vo.PrincipalVo;
import com.quickcancellation.crm.service.ISaleareaService;
import com.quickcancellation.crm.service.IUserinfoService;
import com.quickcancellation.crm.utils.DateUtils;
import com.quickcancellation.crm.utils.PageUtils;
import com.quickcancellation.crm.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-02-28
 */
@CrossOrigin
@RestController
@SaCheckLogin
@Api(tags = "营销区域管理")
@SaCheckRole("salearea")
@RequestMapping("/salearea")
public class SaleareaController {

    private final static Logger logger = LoggerFactory.getLogger(SaleareaController.class);

    @Resource
    private ISaleareaService saleareaService;

    @Resource
    HttpServletRequest request;

    @Resource
    private IUserinfoService userinfoService;

    @PostMapping("list")
    @LogAspect()
    @ApiOperation("营销区域列表")
    public ResponseDto list(@RequestBody PageParamsDto<Salearea> pageParams) {

        PageDto<Salearea> pageDto = PageUtils.getPageParams(pageParams);


        List<Map> saleAreaList = saleareaService.selectSaleAreaList(pageDto);


        return ResponseUtil.success(saleAreaList.size(),saleAreaList);

    }

    @PostMapping("save")
    @LogAspect()
    @ApiOperation("添加营销区域")
    public ResponseDto save(@RequestBody Salearea salearea) {
        BaseMapper<Salearea> mapper = saleareaService.getBaseMapper();

        QueryWrapper<Salearea> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.hasLength(salearea.getSaleName())){
            return ResponseUtil.fail("-5001","请输入营销区域名称");
        }
        if (!StringUtils.hasLength(salearea.getSaleCode())){
            return ResponseUtil.fail("-5001","请输入营销区域编码");
        }
        // 判断编码是否重复
        queryWrapper.eq("saleCode",salearea.getSaleCode());
        if (mapper.exists(queryWrapper)){
            return ResponseUtil.fail("-5002","营销区域编码重复");
        }
        queryWrapper.clear();
        queryWrapper.eq("saleName",salearea.getSaleName());
        if (mapper.exists(queryWrapper)){
            return ResponseUtil.fail("-5002","营销区域名称重复");
        }

        salearea.setCreateTime(DateUtils.getNow());
        salearea.setUpdateTime(DateUtils.getNow());

        String userId = userinfoService.getUserInfoByRequest(request).getId();

        salearea.setCreateOp(userId);
        salearea.setUpdateOp(userId);

        boolean result = saleareaService.save(salearea);

        return ResponseUtil.ok(result);

    }

    @PostMapping("delete")
    @LogAspect()
    @ApiOperation("删除营销区域")
    public ResponseDto deleteById(@RequestBody Salearea salearea) {
        if (salearea.getId() == null) {
            return ResponseUtil.fail("id为空");
        }

        return ResponseUtil.ok(saleareaService.removeById(salearea.getId()));
    }


    @PostMapping("update")
    @LogAspect()
    @ApiOperation("更新营销区域")
    public ResponseDto updateById(@RequestBody Salearea salearea) {
        if (salearea.getId() == null) {
            return ResponseUtil.fail("id为空");
        }

        salearea.setSaleCode(null);
        salearea.setSaleName(null);
        salearea.setCreateOp(null);
        salearea.setCreateTime(null);

        // 获取创建人
        salearea.setUpdateOp(userinfoService.getUserInfoByRequest(request).getId());
        salearea.setUpdateTime(DateUtils.getNow());


        return ResponseUtil.ok(saleareaService.updateById(salearea));
    }

    @PostMapping("getprincipalList")
    @LogAspect()
    @ApiOperation("获取负责人列表")
    public ResponseDto getPrincipalList(){

        List<PrincipalVo> principalVoList =  userinfoService.getprincipalList();


        return ResponseUtil.success(principalVoList.size(),principalVoList);
    }

}
